package com.db.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.db.common.annotation.RequiredLog;
import com.db.common.exception.ServiceException;
import com.db.common.vo.CheckBox;
import com.db.common.vo.PageObject;
import com.db.common.vo.SysRoleMenuVo;
import com.db.dao.SysRoleDao;
import com.db.dao.SysRoleMenuDao;
import com.db.dao.SysUserRoleDao;
import com.db.pojo.SysRole;
import com.db.service.SysRoleService;

@Service
public class SysRoleServiceImpl implements SysRoleService{

	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;
	@Autowired
	private SysUserRoleDao sysUserRoleDao;
	
	@Override
	@RequiredLog("查询角色")
	public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页码值无效");
		int rowCount=sysRoleDao.getRowCount(name);
		if(rowCount==0)
			throw new ServiceException("没有找到对应的记录");
		int pageSize=3;
		int startIndex=(pageCurrent-1)*pageSize;
		List<SysRole> records=sysRoleDao.findPageObjects(name, startIndex, pageSize);
		return new PageObject<>(pageCurrent, pageSize, rowCount, records);
	}

	@Override
	@RequiredLog("删除角色")
	public int deleteObject(Integer id) {
		if(id==null||id<=0)
			throw new IllegalArgumentException("请先选择");
		sysRoleMenuDao.deleteObjectsByRoleId(id);
		sysUserRoleDao.deleteObjectsByRoleId(id);
		int rows=sysRoleDao.deleteObject(id);
		if(rows==0)
			throw new ServiceException("此记录可能不存在");
		return rows;
	}

	@Override
	@RequiredLog("添加角色")
	public int saveObject(SysRole entity, Integer[] menuIds) {
		if(entity==null)
			throw new IllegalArgumentException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getName()))
			throw new IllegalArgumentException("角色名不允许为空");
		if(menuIds==null||menuIds.length==0)
			throw new ServiceException("必须为角色分配权限");
		int rows=sysRoleDao.insertObject(entity);
		sysRoleMenuDao.insertObject(entity.getId(), menuIds);
		return rows;
	}

	@Override
	public SysRoleMenuVo findObjectById(Integer id) {
		if(id==null||id<=0)
			throw new ServiceException("id的值不合法");
		SysRoleMenuVo result=sysRoleDao.findObjectById(id);
		if(result==null)
			throw new ServiceException("此记录已经不存在");
		return result;
	}

	@Override
	@RequiredLog("更新角色")
	public int updateObject(SysRole entity, Integer[] menuIds) {
		if(entity==null)
			throw new ServiceException("更新的对象不能为空");
		if(entity.getId()==null)
			throw new ServiceException("id的值没能为空");
		if(StringUtils.isEmpty(entity.getName()))
			throw new ServiceException("角色名不能为空");
		if(menuIds==null||menuIds.length==0)
			throw new ServiceException("必须为角色指定一个权限");
		int rows=sysRoleDao.updateObject(entity);
		if(rows==0)
			throw new ServiceException("对象可能已经不存在");
		sysRoleMenuDao.deleteObjectsByRoleId(entity.getId());
		sysRoleMenuDao.insertObject(entity.getId(), menuIds);
		return rows;
	}

	@Override
	public List<CheckBox> findRoles() {
		// TODO Auto-generated method stub
		return sysRoleDao.findObjects();
	}

}
